﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
using System.Transactions;

public partial class Main_ServiceInvoice_ServiceMasterEdit : System.Web.UI.Page
{
    string sql = "";
    
    Datautility du = new Datautility();
    protected void Page_Load(object sender, EventArgs e)
    {
       // Service = Request.QueryString["Servicekey"].ToString();
         if (Session["userid"] == null)
        {
            Response.Redirect("~/SessionExpired.aspx");
        }
        if (!IsPostBack)
        {       
            BindTaxablity();
            BindCategory();
            BindDdlSubclass();
            BindDdlAdditionTax();
            BindDdlHeadofAccount();
            BindServiceform(Request.QueryString["Servicekey"].ToString());
        }
    }

    protected void BindTaxablity()
    {
        DdlTaxablity.Items.Insert(0, new ListItem("Taxable", "0"));
        DdlTaxablity.Items.Insert(1, new ListItem("Non-Taxable", "1"));
        DdlTaxablity.Items.Insert(2, new ListItem("Expense Reimbursement", "2"));
        DdlTaxablity.SelectedIndex = 0;
    }

    protected void BindDdlSubclass()
    {
        DdlSubclass.Items.Insert(0, new ListItem("Domestic", "1"));
        DdlSubclass.Items.Insert(1, new ListItem("Export", "2"));
        DdlSubclass.Items.Insert(2, new ListItem("Exempted", "3"));
        DdlSubclass.Items.Insert(3, new ListItem("Others", "4"));
    }

    protected void BindDdlAdditionTax()
    {
        DataTable dt = du.getdatausingprocedure("USP_IVC_GetAdditionalTax");
        if (dt.Rows.Count > 0)
        {
            DdlAdditionTax.DataSource = dt;
            DdlAdditionTax.DataTextField = "MAINCLASSNAME";
            DdlAdditionTax.DataValueField = "mainclassid";
            DdlAdditionTax.DataBind();
            DdlAdditionTax.Items.Insert(0, new ListItem("", "0"));
            DdlAdditionTax.SelectedIndex = 0;
        }
    }

    protected void BindCategory()
    {
        sql = "select SERVICECATEGORYID, SERVICECATEGORY from IVC_SERVICECATEGORY";
        DataTable dt = du.getdata(sql);
        if (dt.Rows.Count > 0)
        {
            DdlCategory.DataTextField = "SERVICECATEGORY";
            DdlCategory.DataValueField = "SERVICECATEGORYID";
            DdlCategory.DataSource = dt;
            DdlCategory.DataBind();
            DdlCategory.Items.Insert(0, new ListItem("", "0"));
        }
    }

    protected void BindDdlHeadofAccount()
    {
        DataTable dt = du.getdatausingprocedure("USP_IVC_GetHeadOfAccountServiceMaster");
        if (dt.Rows.Count > 0)
        {

            DdlHeadofAccount.DataSource = dt;
            DdlHeadofAccount.DataTextField = "LNAME";
            DdlHeadofAccount.DataValueField = "LID";
            DdlHeadofAccount.DataBind();
            DdlHeadofAccount.Items.Insert(0, new ListItem("", "0"));
            DdlHeadofAccount.SelectedIndex = 0;
        }
    }

    protected void BindBasisOfTaxablity(string taxablity)
    {
        if (taxablity == "0")
        {
            PanelTaxablity.Visible = true;
            PanelExpense.Visible = false;
            //PanelNonTaxable.Visible = false;
            DdlAdditionTax.Enabled = true;
            TxtServicetax.Enabled = true;
            btnService.Enabled = true;
            PanelTax.Enabled = true;
            GetCurrentServiceTaxRate(DateTime.Now.ToShortDateString());

        }
        if (taxablity == "1")
        {
            PanelExpense.Visible = false;
            //PanelNonTaxable.Visible = true;
            PanelTaxablity.Visible = false;
            DdlAdditionTax.Enabled = false;

            TxtServicetax.Text = "";
            TxtServicetax.Enabled = false;
            btnService.Enabled = false;
            PanelTax.Enabled = false;
            TxtRate.Text = string.Empty;
            TxtPeCess.Text = string.Empty;
            TxtHecess.Text = string.Empty;

            btnService.Enabled = false;
            DdlAdditionTax.SelectedValue = "0";
            TxtTaxRate.Text = string.Empty;
            TxtExempted.Text = string.Empty;
            PanelExempted.Visible = false;

            hidServiceId.Value = string.Empty;
        }
        if (taxablity == "2")
        {
            //PanelNonTaxable.Visible = false;
            PanelTaxablity.Visible = false;
            PanelExpense.Visible = true;

            TxtServicetax.Text = "";
            DdlAdditionTax.Enabled = false;
            TxtServicetax.Enabled = false;
            btnService.Enabled = false;
            PanelTax.Enabled = false;

            btnService.Enabled = false;
            DdlAdditionTax.SelectedValue = "0";
            TxtTaxRate.Text = string.Empty;
            TxtExempted.Text = string.Empty;
            PanelExempted.Visible = false;
            hidServiceId.Value = string.Empty;
        }
    }

    private void GetCurrentServiceTaxRate(string strCurrentDate)
    {

        if (strCurrentDate != string.Empty)
        {
            string[] strTemp = strCurrentDate.ToString().Split('/');
            string strDate = strTemp[1] + "/" + strTemp[0] + "/" + strTemp[2];

            SqlParameter[] sparam ={
                                       new SqlParameter("@CURRENTDATE",strDate)
                                   };
            DataTable DTCurrentST = du.getdatausingprocedure("USP_IVC_GetCurrentServiceTaxRate", sparam);
            if (DTCurrentST.Rows.Count > 0)
            {
                TxtRate.Text = string.Format("{0:0.00}", Convert.ToDouble(DTCurrentST.Rows[0]["ServiceTax"].ToString())).ToString();
                TxtPeCess.Text = string.Format("{0:0.00}", Convert.ToDouble(DTCurrentST.Rows[0]["PCess"].ToString())).ToString();
                TxtHecess.Text = string.Format("{0:0.00}", Convert.ToDouble(DTCurrentST.Rows[0]["HCess"].ToString())).ToString();
            }
        }
    }

    protected void bindtax()
    {

        SqlParameter[] parm = {
                                  new SqlParameter("@mainclassid",DdlAdditionTax.SelectedValue) 
                              };
        DataTable dt = du.getdatausingprocedure("usp_ivc_Bindtaxrate", parm);
        if (dt.Rows.Count > 0)
        {
            TxtTaxRate.Text = dt.Rows[0]["DEFTAXRATE"].ToString();
        }
        else
        {
            TxtTaxRate.Text = string.Empty;
        }
    }

    protected void DdlTaxablity_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindBasisOfTaxablity(DdlTaxablity.SelectedValue);
    }

    protected void VisiblityBasisAdditionalTax(string Additional)
    {
        if (DdlAdditionTax.SelectedValue != "0")
        {
            bindtax();
        }
        if (Additional == "1" || Additional == "2")
        {
            PanelExempted.Visible = false;
        }
        if (Additional == "3" || Additional == "4" || Additional == "5")
        {
            PanelExempted.Visible = true;
        }
    }

    protected void DdlAdditionTax_SelectedIndexChanged(object sender, EventArgs e)
    {
        VisiblityBasisAdditionalTax(DdlAdditionTax.SelectedIndex.ToString());
    }

    protected void Exemptedpanelvisible()
    {
        sql = "Select MAINCLASSNAME, Mainclassid from ACC_TAXMAINCLASS where TAXTYPEID = 11";
        DataTable dt = du.getdata(sql);
        if (dt.Rows.Count > 0)
        {
            PanelExempted.Visible = true;
        }
        else
        {
            PanelExempted.Visible = false;
        }
    }

    protected void SubclassVisiblity(string Subclass)
    {
        if (Subclass == "0")
        {
            GetCurrentServiceTaxRate(DateTime.Now.ToShortDateString());
            TxtTaxRate.Text = "0.00";
        }
        if (Subclass == "1")
        {
            TxtRate.Text = "0.00";
            TxtPeCess.Text = "0.00";
            TxtHecess.Text = "0.00";
            TxtTaxRate.Text = "0.00";
        }
        if (Subclass == "2")
        {
            TxtRate.Text = "0.00";
            TxtPeCess.Text = "0.00";
            TxtHecess.Text = "0.00";
            TxtTaxRate.Text = "0.00";
        }
        if (Subclass == "3")
        {
            TxtRate.Text = "0.00";
            TxtPeCess.Text = "0.00";
            TxtHecess.Text = "0.00";
            TxtTaxRate.Text = "0.00";
        }
    }

    protected void DdlSubclass_SelectedIndexChanged(object sender, EventArgs e)
    {
        SubclassVisiblity(DdlSubclass.SelectedValue);
    }


    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("ServiceMaster.aspx?action=view");
    }

    protected void BindServiceform(string Service)
    {
        sql = "select * from IVC_SERVICEMASTER where Serviceid =" + Service;
        DataTable dt = du.getdata(sql);
        if (dt.Rows.Count > 0)
        {
            TxtServiceName.Text = dt.Rows[0]["SERVICENAME"].ToString();
            TxtAdditionDescription.Text = dt.Rows[0]["ADDDESC"].ToString();
            DdlHeadofAccount.SelectedValue = dt.Rows[0]["LEDGERID"].ToString();
            DdlTaxablity.SelectedValue = dt.Rows[0]["TAXABILITY"].ToString();
            BindBasisOfTaxablity(dt.Rows[0]["TAXABILITY"].ToString()); // Visiblity of taxablity 

            DdlCategory.SelectedValue = dt.Rows[0]["SERVICECATEGORYID"].ToString();
            DdlSubclass.SelectedValue = dt.Rows[0]["SUBCLASS"].ToString();
            SubclassVisiblity(dt.Rows[0]["SUBCLASS"].ToString());
            Radioexpense.SelectedValue = dt.Rows[0]["EXPENSEREIMURSEMENT"].ToString();
            TxtService.Text = dt.Rows[0]["servicechargerate"].ToString();
            hidServiceId.Value = dt.Rows[0]["SERVICECLASSIFICATIONID"].ToString();
            string sql1 = "select serviceid, servicename from Svt_servicemaster where serviceid =" + dt.Rows[0]["SERVICECLASSIFICATIONID"].ToString(); //Service Classification

            DataTable dtsetrvice = du.getdata(sql1);

            if (dtsetrvice.Rows.Count > 0)
            {
                TxtServicetax.Text = dtsetrvice.Rows[0]["servicename"].ToString();
            }


            if (dt.Rows[0]["TAXABILITY"].ToString() == "0")
            {
                if (dt.Rows[0]["SUBCLASS"].ToString() == "1")
                {
                    GetCurrentServiceTaxRate(DateTime.Now.ToShortDateString());
                }
            }

            if (dt.Rows[0]["TAXABILITY"].ToString() == "1")
            {
                TxtServicetax.Text = string.Empty;
            }


            if (dt.Rows[0]["TAXABILITY"].ToString() == "2")
            {
                TxtServicetax.Text = string.Empty;
            }

            if (Convert.ToDouble(dt.Rows[0]["VATEXMPRATE"].ToString()) > 0)
            {
                PanelExempted.Visible = true;
                TxtExempted.Text = dt.Rows[0]["VATEXMPRATE"].ToString();
            }
            else
            {
                PanelExempted.Visible = false;
            }

            DdlAdditionTax.SelectedValue = dt.Rows[0]["TAXTYPEID"].ToString();

            VisiblityBasisAdditionalTax(DdlAdditionTax.Items.IndexOf(DdlAdditionTax.Items.FindByValue(dt.Rows[0]["TAXTYPEID"].ToString())).ToString());

            SqlParameter[] parm = {
                                  new SqlParameter("@mainclassid", dt.Rows[0]["TAXTYPEID"].ToString()) 
                              };
            DataTable dttax = du.getdatausingprocedure("usp_ivc_Bindtaxrate", parm);
            if (dttax.Rows.Count > 0)
            {
                TxtTaxRate.Text = dttax.Rows[0]["DEFTAXRATE"].ToString();
            }
            else
            {
                TxtTaxRate.Text = string.Empty;
            }

            RadioApplyTax.SelectedValue = dt.Rows[0]["TAXCALON"].ToString();
        }
    }

    protected void btnupdate_Click(object sender, EventArgs e)
    {
        try
        {
            using (TransactionScope scope = new TransactionScope())
            {
                double exempted = 0;
                if (TxtExempted.Text != string.Empty)
                {
                    exempted = Convert.ToDouble(TxtExempted.Text);
                }
                SqlParameter[] parm = {
                                  new SqlParameter("@SERVICEID", Request.QueryString["Servicekey"].ToString()),
                                  new SqlParameter("@SERVICENAME", TxtServiceName.Text.Trim()),
                                  new SqlParameter("@SERVICECATEGORYID", DdlCategory.SelectedValue),
                                  new SqlParameter("@ADDDESC",TxtAdditionDescription.Text.Trim()),
                                  new SqlParameter("@TAXABILITY",DdlTaxablity.SelectedValue),
                                  new SqlParameter("@LEDGERID",DdlHeadofAccount.SelectedValue),
                                  new SqlParameter ("@SERVICECLASSIFICATIONID",hidServiceId.Value),
                                  new SqlParameter("@EXPENSEREIMURSEMENT",Radioexpense.SelectedValue),
                                  new SqlParameter("@TAXTYPEID",DdlAdditionTax.SelectedValue),
                                  new SqlParameter("@TAXCALON",RadioApplyTax.SelectedValue),
                                  new SqlParameter("@SUBCLASS",DdlSubclass.SelectedValue),
                                  //new SqlParameter("@ABATEMENTYN",""),
                                  new SqlParameter("@VATEXMPRATE",exempted),
                                  new SqlParameter("@SERVICECHARGERATE",TxtService.Text.Trim()),
                                  new SqlParameter("@ENAME",Session["username"].ToString())

                              };
                //lblMsg.Text = DdlAdditionTax.SelectedValue;
                du.executeprocedure("usp_IVC_UpdateServicemaster", parm);
                scope.Complete();
                lblMsg.Attributes.Remove("class");
                lblMsg.Attributes.Add("class", "true");
                lblMsg.Text = "Service Updated";
            }
        }
        catch (Exception ex)
        {
            lblMsg.Text = ex.Message;
        }
    }


}
